package com.titan.single.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

/**
 * JSON转字符串工具类
 */
@Slf4j
public final class JsonUtils {

    private static final ObjectMapper OBJECTMAPPER = new ObjectMapper();

    //驼峰命名转换
    private static final  ObjectMapper objectMapperName = new ObjectMapper();

    static {
        OBJECTMAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        // 注册时间处理模块
        OBJECTMAPPER.registerModule(new JavaTimeModule());
        OBJECTMAPPER.disable(JsonGenerator.Feature.ESCAPE_NON_ASCII);
        //ObjectMapper忽略未知字段
        OBJECTMAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        // 属性命名策略：将下划线命名转换为驼峰命名
        objectMapperName.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        // 未知属性，避免因为额外字段导致反序列化失败
        objectMapperName.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    /**
     * 对象转json字符串
     */
    public static String objToString(Object obj) {
        try {
            return OBJECTMAPPER.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            log.error("对象转json字符串异常", e);
        }
        return null;
    }

    /**
     * 字符串转对象
     */
    public static <T> T stringToObj(String content, Class<T> clazz) {
        try {
            return OBJECTMAPPER.readValue(content, clazz);
        } catch (IOException e) {
            log.error("json字符串转对象异常", e);
        }
        return null;
    }

    /**
     * 对象转字节 序列化
     */
    public static byte[] objToByte(Object obj) {
        try {
            return OBJECTMAPPER.writeValueAsBytes(obj);
        } catch (JsonProcessingException e) {
            log.error("对象转json字符串异常", e);
        }
        return null;
    }



    /**
     * 字符串转对象-驼峰命名转换
     *
     * @param content
     * @param <T>
     * @return
     */
    public static <T> T stringToObjName(String content, TypeReference<T> typeReference) {
        try {
            return objectMapperName.readValue(content, typeReference);
        } catch (IOException e) {
            log.error("json字符串转对象异常", e);
        }
        return null;
    }


    /**
     * 字符串转对象-驼峰命名转换
     *
     * @param content
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T stringToObjName(String content, Class<T> clazz) {
        try {
            return objectMapperName.readValue(content, clazz);
        } catch (IOException e) {
            log.error("json字符串转对象异常", e);
        }
        return null;
    }


}
